package com.base.controller.api;

import com.base.po.param.NetState;
import com.base.po.voip.Subscribers;
import com.base.po.voip.UserAuthInfo;
import com.base.service.SubscribersService;
import com.base.service.UserAuthInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * 用户信息控制器
 *
 * @author Administrator
 *
 */
@Log4j
@Api(tags = "实名认证信息")
@Controller
@RequestMapping("/userInfo")
public class UserAuthController{
	@Autowired
	private SubscribersService subscribersService;
	@Autowired
	private UserAuthInfoService userAuthInfoService;

	@ApiOperation(value = "是否实名认证")
	@RequestMapping(value = "/isUserAuth", method = RequestMethod.POST)
	public @ResponseBody NetState isUserAuth(@ApiParam("用户Id") @RequestParam String userId) {
		NetState netState = new NetState();

		// 用户信息
		Subscribers user = subscribersService.selectByPrimaryKey(userId);
		if (user == null) {
			netState.setErrorCode(NetState.ACCOUNT_ERROR);
			netState.setData("未找到该用户");
		}else {
			Boolean isUserAuth = subscribersService.isUserAuth(userId);
			if(isUserAuth){
				netState.setErrorCode(NetState.SUCCESS_CONN);
				netState.setData("该身份证号已认证");
				netState.setJson(isUserAuth);
			}else {
				netState.setErrorCode(NetState.SUCCESS_CONN);
				netState.setData("该身份证号未认证");
				netState.setJson(isUserAuth);
			}
		}

		return netState;
	}

	@ApiOperation(value = "实名认证")
	@RequestMapping(value = "/userAuthForIdCardImg", method = RequestMethod.POST)
	public @ResponseBody NetState userAuth(@ApiParam("用户Id") @RequestParam String userId,
										   @ApiParam("姓名") @RequestParam String userRealName,
										   @ApiParam("身份证号") @RequestParam String idcardNumber,
										   @ApiParam("身份证正面") @RequestParam String frontImg,
										   @ApiParam("身份证反面") @RequestParam String backImg) {
		NetState netState = new NetState();

		// 用户实名信息
		UserAuthInfo userAuthInfo = new UserAuthInfo();
		// 用户信息
		Subscribers user = subscribersService.selectByPrimaryKey(userId);
		if (user == null) {
			netState.setErrorCode(NetState.ACCOUNT_ERROR);
			netState.setData("未找到该用户");
		} else if (user.getDatastatus() == false) {
			netState.setErrorCode(NetState.ACCOUNT_ERROR);
			netState.setData("该用户被禁用");
		} else if (user.getIsUserAuth() == 1) {
			netState.setErrorCode(NetState.DATA_RE);
			netState.setData("已实名认证成功");
		} else {
			if (StringUtils.isBlank(userRealName)) {
				netState.setErrorCode(NetState.PARAMS_ERROR);
				netState.setData("请填写姓名");
				return netState;
			} else {
				userAuthInfo.setUserRealName(userRealName);
			}
			if (StringUtils.isBlank(idcardNumber)) {
				netState.setErrorCode(NetState.PARAMS_ERROR);
				netState.setData("请填写身份证号码");
				return netState;
			} else {
				String idcardRegular = "(^[1-9]\\d{5}(18|19|20|21)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|";
				if (idcardNumber.matches(idcardRegular)) {
					//当身份证号码不为空时，开始检测该号码是否已被验证过
					int count = userAuthInfoService.queryByIdcardNumber(idcardNumber);
					if (count == 0) {
						userAuthInfo.setIdcardNumber(idcardNumber);
					} else {
						netState.setErrorCode(NetState.PARAMS_ERROR);
						netState.setData("该身份证号已认证，无法再次认证");
						return netState;
					}
				} else {
					netState.setErrorCode(NetState.PARAMS_ERROR);
					netState.setData("身份证号码格式错误");
					return netState;
				}
			}
			if (StringUtils.isBlank(frontImg)) {
				netState.setErrorCode(NetState.PARAMS_ERROR);
				netState.setData("请上传身份证人像面");
				return netState;
			} else {
				userAuthInfo.setFrontImg(frontImg);
			}
			if (StringUtils.isBlank(backImg)) {
				netState.setErrorCode(NetState.PARAMS_ERROR);
				netState.setData("请上传身份证国徽面");
				return netState;
			} else {
				userAuthInfo.setBackImg(backImg);
			}
			userAuthInfo.setId(user.getId());//填入用户id
			userAuthInfo.setIdentifyDate(new Date());//填入身份证认证保存信息时间
			try {
				//将此次传递的身份证验证信息进行保存
				int row = userAuthInfoService.insert(userAuthInfo);
				if (row == 1) {
					netState.setErrorCode(NetState.SUCCESS_CONN);
					netState.setData("身份证认证成功");
					subscribersService.updateIsUserAuthById(user.getId());
				} else {
					netState.setErrorCode(NetState.PARAMS_ERROR);
					netState.setData("认证信息保存失败");
				}
			} catch (Exception e) {
				e.printStackTrace();
				netState.setErrorCode(NetState.PARAMS_ERROR);
				netState.setData("身份证信息保存失败");
			}
		}

		return netState;
	}
}
